package model;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public class Flight {
	private List<FlightLeg> legs;
	private BigDecimal totalCost;

	// --- Getters,Setters,Constructors ----------------------------------------
	public Flight(List<FlightLeg> legs, BigDecimal totalCost) {
		if (legs == null && legs.size() == 0) {
			throw new IllegalArgumentException(
					"Flight must have at least one leg.");
		}
		this.legs = legs;
		this.totalCost = totalCost;
	}

	public List<FlightLeg> getLegs() {
		return legs;
	}

	public BigDecimal getTotalCost() {
		return totalCost;
	}

	// --- Methods -------------------------------------------------------------
	public FlightLeg getFirstLeg() {
		return legs.get(0);
	}

	public FlightLeg getLastLeg() {
		return legs.get(legs.size() - 1);
	}

	public Airport getDepartureAirport() {
		return getFirstLeg().getDepartureAirport();
	}

	public Airport getArrivalAirport() {
		return getLastLeg().getArrivalAirport();
	}

	public boolean isNonStop() {
		return legs.size() == 1;
	}

	public int getNumbersOfLeg() {
		return legs.size();
	}

	public long getTotalTravelTime() {
		Date departureDate = getFirstLeg().getDepartureDate();
		Date arrivalDate = getLastLeg().getArrivalDate();
		if (arrivalDate.compareTo(departureDate) <= 0) {
			throw new IllegalArgumentException(
					"Departure date must be before arrival date");
		}
		return arrivalDate.getTime()-departureDate.getTime();
	}

}
